package com.centling.smartSealForPhone.fragment;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.EditText;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.activity.AlarmDetailActivity;
import com.centling.smartSealForPhone.adapter.RvAlarmListAdapter;
import com.centling.smartSealForPhone.adapter.RvStampListAdapter;
import com.centling.smartSealForPhone.bean.AlarmListBean;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.OkHttpUtil;
import com.centling.smartSealForPhone.utils.ShowToast;
import com.centling.smartSealForPhone.widget.AutoRecyclerView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

public class AlarmListFragment extends TitleBarFragment {

    private EditText etInfo;
    private AlarmListBean alarmListBean;
    private AutoRecyclerView rvAlarm;
    private List<AlarmListBean.RowsEntity> alarmList=new ArrayList<>();
    private PtrClassicFrameLayout ptrAlarmList;
    private int pageIndex = 0;

    public AlarmListFragment() {
    }

    public static AlarmListFragment getInstance(){
        return new AlarmListFragment();
    }

    @Override
    public int setLayoutResource() {
        return R.layout.fragment_alarm_list;
    }

    @Override
    public void findViewInThisFunction(View rootView) {
        setTitleBarText("报警信息列表");
        setTitleBarLeft(R.drawable.ic_action_back);
        setTitleBarLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });

        initView(rootView);
        initData();
    }

    private void initView(View view){
        ptrAlarmList= view.findViewById(R.id.ptr_alarm_list);
        rvAlarm= view.findViewById(R.id.rv_alarm_list);
    }

    private void initData(){
        rvAlarm.setLayoutManager(new GridLayoutManager(mContext, 1));
        RvAlarmListAdapter adapter=new RvAlarmListAdapter(mContext, alarmList);
        rvAlarm.setAdapter(adapter);
        adapter.setOnItemClickListener(new RvStampListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int pos) {
                Intent intent=new Intent(mContext, AlarmDetailActivity.class);
                intent.putExtra("sealName", alarmList.get(pos).getSealName());
                intent.putExtra("operatorName", alarmList.get(pos).getOperatorName());
                intent.putExtra("startTime", alarmList.get(pos).getAlarmTimeStartStr());
                intent.putExtra("endTime", alarmList.get(pos).getAlarmTimeEndStr());
                intent.putExtra("latitude", alarmList.get(pos).getLatitude());
                intent.putExtra("longitude", alarmList.get(pos).getLongitude());
                intent.putExtra("alarmLocation", alarmList.get(pos).getAlarmLocation());
                startActivity(intent);
            }
        });
        ptrAlarmList.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                sendRequestToGetAlarmList(true);
            }
        });
        rvAlarm.setLoadDataListener(new AutoRecyclerView.LoadDataListener() {
            @Override
            public void onLoadMore() {
                sendRequestToGetAlarmList(false);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        ptrAlarmList.post(new Runnable() {
            @Override
            public void run() {
                ptrAlarmList.autoRefresh();
            }
        });
    }

    private void sendRequestToGetAlarmList(final boolean isRefresh){
        if (isRefresh) {
            pageIndex = 0;
        }
        getEnqueue(String.format(Locale.CHINA, UrlConstants.GET_ALERT_INFO, pageIndex),
                new OkHttpUtil.NetCallBack() {
                    @Override
                    public void onSucceed(String json) {
                        if(isRefresh){
                            alarmList.clear();
                            rvAlarm.setLoadDataListener(new AutoRecyclerView.LoadDataListener() {
                                @Override
                                public void onLoadMore() {
                                    sendRequestToGetAlarmList(false);
                                }
                            });
                        }
                        alarmListBean=new Gson().fromJson(json, AlarmListBean.class);
                        alarmList.addAll(alarmListBean.getRows());
                        rvAlarm.getAdapter().notifyDataSetChanged();
                        if (alarmListBean != null && alarmListBean.getRows().size() < 10) {
                            rvAlarm.loadMoreComplete(true);
                        } else {
                            rvAlarm.loadMoreComplete(false);
                        }
                        ptrAlarmList.refreshComplete();
                        pageIndex++;
                    }

                    @Override
                    public void onFailed(String reason) {
                        ShowToast.shortTime(reason);
                        ptrAlarmList.refreshComplete();
                        rvAlarm.loadMoreComplete(false);
                    }
                });
    }
}
